package net.zzy.pbbot.plugin.dxx.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.dxx.entity.CommitBody;
import net.zzy.pbbot.plugin.dxx.entity.DxxDaoEntity;
import net.zzy.pbbot.plugin.dxx.entity.DxxResp;
import net.zzy.pbbot.plugin.dxx.mapper.DxxMapper;
import net.zzy.pbbot.util.HttpsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static net.zzy.pbbot.finalfield.Api.DXX_COMMIT;
import static net.zzy.pbbot.finalfield.FinalStr.NEED_ADD_DXX_INFO;
import static net.zzy.pbbot.finalfield.FinalStr.PHONE_USER_AGENT;

/**
 * @author zhuZhaoYang
 * @date 2021/9/29 18:14
 */
@Component
public class DxxService {

    private DxxMapper dxxMapper;

    @Autowired
    public void setDxxMapper(DxxMapper dxxMapper) {
        this.dxxMapper = dxxMapper;
    }

    @SneakyThrows
    public Msg commitDxx(String qq) {
        DxxDaoEntity dxxDaoEntity = dxxMapper.selectOne(new QueryWrapper<DxxDaoEntity>().eq("qq", qq));
        if (dxxDaoEntity == null) {
            return Msg.builder().text(NEED_ADD_DXX_INFO);
        }
        Map<String, String> headers = new HashMap<>();
        headers.put("token", dxxDaoEntity.getToken());
        headers.put("User-Agent", PHONE_USER_AGENT);
        CommitBody commitBody = new CommitBody();
        commitBody.setName(dxxDaoEntity.getName());
        commitBody.setTel(dxxDaoEntity.getTel());
        commitBody.setOrg("#2#5942#6068#6885#");
        commitBody.setLastOrg("6885");
        commitBody.setOrgName("计算机科学与技术专业2019级5班团支部");
        commitBody.setAllOrgName("#高校#成都信息工程大学#计算机学院团委#计算机科学与技术专业2019级5班团支部#");
        String post = HttpsUtils.post(DXX_COMMIT, JSON.toJSONString(commitBody), headers);
        DxxResp dxxResp = JSON.parseObject(post, DxxResp.class);
        return Msg.builder().text(dxxResp.getMsg());
    }

    public Msg cancelBangDxx(String qq) {
        DxxDaoEntity dxxDaoEntity = dxxMapper.selectOne(new QueryWrapper<DxxDaoEntity>().eq("qq", qq));
        if (dxxDaoEntity == null) {
            return Msg.builder().text(NEED_ADD_DXX_INFO);
        }
        dxxMapper.delete(new QueryWrapper<DxxDaoEntity>().eq("qq", qq));
        return Msg.builder().text("取消绑定青年大学习成功!");
    }

    public Msg bangDxx(String[] messages, String qq) {

        String name = messages[1];
        String tel = messages[2];
        String token = messages[3];
        DxxDaoEntity dxxDaoEntity = dxxMapper.selectOne(new QueryWrapper<DxxDaoEntity>().eq("qq", qq));
        Msg msg;
        if (dxxDaoEntity != null) {
            msg = Msg.builder().text("此QQ已绑定信息,你的指令将更新你的QQ绑定的信息\n");
            dxxDaoEntity.setName(name);
            dxxDaoEntity.setToken(token);
            dxxDaoEntity.setTel(tel);
            dxxMapper.updateById(dxxDaoEntity);
            msg.text("绑定成功！请确认你的信息：\n姓名：" + name
                    + "\n电话：" + tel
                    + "\ntoken：" + token
                    + "\nqq：" + qq);
        } else {
            dxxDaoEntity = new DxxDaoEntity();
            dxxDaoEntity.setName(name);
            dxxDaoEntity.setQq(qq);
            dxxDaoEntity.setToken(token);
            dxxDaoEntity.setTel(tel);
            dxxMapper.insert(dxxDaoEntity);
            msg = Msg.builder().text("绑定成功！请确认你的信息：\n姓名：" + name
                    + "\n电话：" + tel
                    + "\ntoken：" + token
                    + "\nqq：" + qq);
        }
        return msg;
    }
}
